<template>
  <div class="certificate-container">
    <div class="tabs">
      <van-tabs v-model="active" @click="menuClick">
        <van-tab title="未支付"></van-tab>
        <van-tab title="已支付"></van-tab>
      </van-tabs>
    </div>
    <div class="list-group" ref="scrollbars">
      <van-list loading-text="加载中" v-model="loading" :finished="finished" :finished-text="listData.length > 0 ? '没有更多了' : ''" @load="initAjaxMethod">
        <div class="match-content" v-for="(item, index) in listData" :key="index">
          <div class="content-img-group-mobile">
            <div style="position: relative;" class="content-img-group">
              <img :src="baseUrl + item.active_log" class="content-img">
            </div>
          </div>
          <div class="detail-mobile">
            <div class="detail-title detail-title-mobile" v-if="active == 1">订单号：{{ item.order_num }}</div>
            <div class="detail-title detail-title-mobile">{{ item.active_title }}</div>
            <div class="detail-content-mobile ellipsis-multiline">{{ item.content }}</div>
            <div class="detail-info">
              <van-icon name="underway-o" size="0.28rem" />
              <span style="margin-left:0.1rem;">{{ item.time_st | dateFrom }}至{{ item.time_ed | dateFrom
                                }}</span>
            </div>
            <div class="detail-info">
              <van-icon name="discount-o" size="0.28rem" />
              <span style="margin-left:0.1rem;">平均分：{{ item.avg_scoring?Number(item.avg_scoring).toFixed(1):0}}</span>
            </div>
            <div class="detail-info">
              <van-icon name="label-o" size="0.28rem" />
              <span style="margin-left:0.1rem;">作品名：{{ item.name }}</span>
            </div>
            <div class="detail-info">
              <van-icon name="gold-coin-o" size="0.28rem" />
              <span style="margin-left:0.1rem;">证书价格：¥{{ item.price }}</span>
            </div>
            <div class="bottom-button">
              <van-button type="info" @click.stop="wxplay(item)" v-if="active == 0">¥{{item.price}} 证书支付</van-button>
              <van-button type="info" @click.stop="wxplayDown(item.id)" v-if="active == 1">证书下载</van-button>
            </div>
          </div>
        </div>
      </van-list>
      <not-find v-if="listData.length == 0"></not-find>
    </div>
    <wechat :callback="wxReadyOkMethod" />
    <van-popup v-model="showImg" :style="{ height: '100%',width:'100%' }">
        <div class="popup-close-header" @click="showImg=false">关闭</div>
      <div style="height: 100vh;background-color: #fff;padding:1rem 0;">
        <img :src="baseUrl+certificate" class="popup-img" alt="">
        <div class="popup-close-group">
          <div class="popup-close-button">长安保存到相册</div>
        </div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import notFind from "@/view/common/notFind";
import wechat from "@/view/common/Wxwechat.vue";
import { getdate, getUserRole } from "@/utils/utils";
import { PullRefresh, List, Toast, Icon, Button, Tag, Tab, Tabs ,Popup} from 'vant';
export default {
  name: "certificate",
  components: {
    notFind, wechat, [PullRefresh.name]: PullRefresh, [List.name]: List, [Icon.name]: Icon,
    [Button.name]: Button, [Tab.name]: Tab, [Tag.name]: Tag, [Tabs.name]: Tabs, [Popup.name]: Popup
  },
  data() {
    return {
      refreshDisabled: false,
      showImg: false,
      loading: false,
      active: 0,
      userRole: getUserRole(),
      finished: true,
      wxReadyOk: false,
      menuId: 3,
      baseUrl: this.$API.ASSET_IMAGES_URL,
      query: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
      listData: [],
      certificate:''
    };
  },
  filters: {
    dateFrom(e) {
      if (!e) return '';
      return getdate(e);
    }
  },
  mounted() {
    this.initAjaxMethod();
  },
  methods: {
    menuClick() {
      this.query.pageIndex = 1;
      this.listData = [];
      this.finished = false;
      this.loading = true;
      this.$refs['scrollbars'].scrollTop = 0;
      this.initAjaxMethod();
    },
    //列表加载
    initAjaxMethod() {
      Toast.loading({ message: "加载中...", forbidClick: true });
      this.$http
        .post(this.$API.USER_CERTIFICATE_LIST, {
          page: this.query.pageIndex,
          pageSize: this.query.pageSize,
          status: this.active
        })
        .then((res) => {
          Toast.clear();
          if (res.code == 1) {
            let list = Array.isArray(res.data.list) ? res.data.list : [];
            let isOver = list.length < this.query.pageSize ? true : false;
            this.finished = isOver;
            this.loading = false;
            this.listData = this.query.pageIndex == 1 ? list : [...this.listData, ...list];
            this.total = res.data.total;
            if (!isOver) {
              this.query.pageIndex++;
            }
          }
        });
    },
    wxplayDown(id) {
      Toast.loading({ message: "加载中...", forbidClick: true });
      this.$http
        .post(this.$API.USER_RANK_CERTIFICATE, { signup_id: id })
        .then((res) => {
          Toast.clear();
          if (res.code == 1) {
            this.certificate=res.data;
            this.showImg=true;
          } else {
            this.$notify({ type: 'warning', message: res.msg });
          }
        });
    },
    wxReadyOkMethod() {
      console.log('js准备好了');
      this.wxReadyOk = true;
    },
    //保存证件号码
    wxplay(row) {
      if (!this.wxReadyOk) {
        return Toast.fail('调起微信支付失败!');
      }
      let that = this;
      Toast.loading({ message: "加载中...", forbidClick: true });
      this.$http
        .post(this.$API.WX_NEWPAY, {
          singup_id: row.id,
          active_id: row.active_id
        })
        .then((result) => {
          console.log('人员详情', result);
          Toast.clear();
          if (result.code == 1) {
            this.$WX.chooseWXPay({
              timestamp: result.data.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
              nonceStr: result.data.nonceStr, // 支付签名随机串，不长于 32 位
              package: result.data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
              signType: result.data.signType, // 微信支付V3的传入RSA,微信支付V2的传入格式与V2统一下单的签名格式保持一致
              paySign: result.data.paySign, // 支付签名
              success: function (res) {
                if (res.errMsg == "chooseWXPay:ok") {
                  Toast.success('支付成功');
                  that.$dialog.confirm({
                    title: '支付提示',
                    message: '您的订单已支付成功,是否现在下载证书!',
                  }).then(r => {
                    that.wxplayDown(row.id)
                  }).catch(ers => { console.log('err', ers) })
                  that.active=1;
                  that.menuClick();
                } else {
                  Toast.fail(res.errMsg);
                }
              },
              cancel: function (res) {
                Toast.fail('取消支付');
              }
            });
          } else if (result.code == 2) {
            this.$dialog.alert({
              title: '支付提示',
              message: '您的订单已支付,无需重复支付!',
            });
          } else if (result.code == 3) {
            Toast.success('支付成功');
            this.onRefresh();
          } else {
            Toast.fail(result.msg);
          }
        })
        .catch((err) => {
          Toast.clear();
          Toast.fail('支付失败！');
        });
    }
  },
};
</script>
<style lang="less">
.certificate-container {
  height: 100%;
  padding-top: 0.88rem;
  position: relative;

  .tabs {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;

    .van-tabs__wrap {
      height: 0.88rem;
    }
    .van-tab__text {
      font-size: 0.3rem;
    }
    .van-tabs__line {
      background-color: #4da5fe;
    }
  }

  .list-group {
    height: 100%;
    padding: 0 0.2rem;
  }

  .detail-tags {
    display: flex;
    justify-content: flex-end;

    .van-tag {
      font-size: 0.24rem;
      padding: 0.1rem 0.3rem;
      border-radius: 4px;
      line-height: 0.24rem;
    }
  }

  .match-content {
    padding: 0.2rem;
    color: #3e3a39;
    display: flex;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
    align-items: center;
    margin-top: 0.2rem;
    cursor: pointer;
    border-radius: 0.2rem;

    .content-img-group-mobile {
      position: relative;
      align-items: center;
      justify-content: center;

      .content-img-group {
        width: 2rem;
        height: 2rem;
        background: #fff;

        .content-img {
          width: 100%;
          height: 100%;
        }
      }
    }

    .detail-content-mobile {
      font-size: 0.24rem;
      color: #3e3a39;
      padding-top: 0.08rem;
    }

    .detail-mobile {
      width: 60%;
      padding-left: 0.3rem;
      flex-grow: 1;
    }

    .detail-title {
      font-size: 0.28rem;
      color: #333;
      font-weight: bold;
    }

    .detail-info {
      display: flex;
      align-items: center;
      padding-top: 0.16rem;

      span {
        font-size: 0.24rem;
        color: #3e3a39;
      }
    }
  }

  .bottom-button {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding-top: 0.2rem;
  }

  .popup-close-group {
    padding: 0.1rem 24%;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
  }
  .popup-close-button {
    height: 0.7rem;
    color: #333;
    line-height: 0.7rem;
    text-align: center;
    font-size: 0.28rem;
  }
  .popup-img{
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .popup-close-header{
    font-size: 0.3rem;
    position: absolute;
    top:0;
    left:0;
    padding:0 0.3rem;
    text-align: right;
    height: 1rem;
    line-height: 1rem;
    width: 100%;
  }
}
</style>